import { getPayOrder, submitPayOrder } from '@/api/pay/pay'
import { useMemberStore } from '@/stores'
import config from '@/utils/config'
import type { SubmitPayParams } from '@/types/order'
import { ref } from 'vue'
const { channelCode, returnUrl } = config
export const usePay = () => {
    // 支付id
    const payId = ref<string | number>(0)
    // 支付金额
    const payAmount = ref<number>(0)
    // 获取支付订单
    const getPayOrderInfo = async (id: string) => {
        try {
            const res: any = await getPayOrder(id)
            if (res.code == 0 && res.data?.length > 0) {
                const data = res.data[0]
                payId.value = data.payId
                payAmount.value = data.amount
            }
        } catch (error) {
            console.log(error)
        }
    }
    // 提交支付订单
    const submitPay = async () => {
        if (payId.value == 0) {
            uni.showToast({ title: '获取支付订单失败', icon: 'none' })
            return
        }
        const member = useMemberStore()
        const par: SubmitPayParams = {
            id: payId.value,
            channelCode,
            displayMode: '',
            returnUrl,
            channelExtras: {
                openid: member.userInfo.openid
            }
        }
        // return
        try {
            uni.showLoading({ title: '获取支付信息...' })
            const res: any = await submitPayOrder(par)
            if (res.data.displayContent) {
                uni.hideLoading()
                const displayContent = JSON.parse(res.data.displayContent)
                requestPayment(displayContent)
            }
        } catch (error) {
            console.log(error)
            uni.hideLoading()
        }
    }
    // 调用微信小程序支付
    const requestPayment = async (payConfig: any) => {
        uni.requestPayment<UniApp.RequestPaymentOptions>({
            provider: "wxpay",
            timeStamp: payConfig.timeStamp,
            nonceStr: payConfig.nonceStr,
            package: payConfig.package,
            signType: payConfig.signType,
            paySign: payConfig.paySign,
            orderInfo: {},
            success(res) {
                uni.showToast({
                    title: '支付成功!'
                })
                uni.setStorageSync('loadData', true)
                uni.setStorageSync('resetHomeFormFlag', true)
                setTimeout(() => {
                    uni.switchTab({ url: '/pages/tabbar/customerOrder/customerOrder' })
                }, 500);
            },
            fail(err) {
                if (err.errMsg === 'requestPayment:fail cancel') {
                    // 取消支付提示
                    uni.showToast({
                        title: '支付已手动取消',
                        icon: 'none'
                    })
                    uni.setStorageSync('loadData', true)
                    uni.setStorageSync('resetHomeFormFlag', true)
                    setTimeout(() => {
                        uni.switchTab({ url: '/pages/tabbar/customerOrder/customerOrder' })
                    }, 500);
                } else {
                    // 支付失败提示
                    uni.showToast({
                        title: '支付失败',
                        icon: 'none'
                    })
                    uni.setStorageSync('loadData', true)
                    uni.setStorageSync('resetHomeFormFlag', true)
                    setTimeout(() => {
                        uni.switchTab({ url: '/pages/tabbar/customerOrder/customerOrder' })
                    }, 500);
                }
            }
        });
    }
    return {
        payId,
        payAmount,
        getPayOrderInfo,
        submitPay
    }
}